/* Copyright (C) 2010 Fredrique Samuels, fredriquesamuels@gmail.com */

/*
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#ifndef TRI_DISPLAY_ENGINE_H_
#define TRI_DISPLAY_ENGINE_H_

#include <GL/gl.h>
#include <GL/glu.h>
#include <iostream>
#include <string>

//#include "entity.h"
#include "graphics.h"
#include "context.h"
#include "scene.h"
#include "events.h"

#define MAX_SELECT_BUFFER_SIZE 20

namespace tri{

class TDisplayEngine;

class TSelectBuffer{
	
	friend class TDisplayEngine;
	
public:
	TSelectBuffer(unsigned int*b = NULL,int size = NULL);
	virtual ~TSelectBuffer();
	
	unsigned int size();
	unsigned int get(unsigned int i);
private:
	unsigned int buf[MAX_SELECT_BUFFER_SIZE];
	unsigned int _size;
};

class TDisplayEngine : public TContext
{
	
public:
	TDisplayEngine();
	virtual ~TDisplayEngine();
	
	virtual void display();
	virtual void resize(int width,int height);
	virtual void init();	
	virtual void keyDown(int key);
	virtual void keyUp(int key);
	virtual void mouseDrag(int x,int y,int xrel,int yrel,int button);
	virtual void mouseMove(int x,int y,int xrel,int yrel);
	virtual void mouseUp(int x,int y,int button);
	virtual void mouseDown(int x,int y,int button);
	
	//scene cache interface
	void addScene(std::string key, TScene* scene);
	void removeScene(std::string key);
	void setActiveScene(std::string key);
	TScene* getScene(std::string key);
	
	//scene cache interface
	void addOverLayScene(std::string key, TScene* scene);
	void removeOverLayScene(std::string key);
	void setActiveOverLayScene(std::string key);
	TScene* getOverLayScene(std::string key);
	
	static bool isPicking();
	//static void getSelection(int l, int t, int r,int b);
	
private:
	
	int pick(TScene* w, TSelectBuffer *_select_buffer );
	
	bool m_isInit;
	TRectangle m_ViewPort;	
	TSceneCache m_MainSceneChache;
	TSelectBuffer m_MainSelectBuffer;
	TSceneCache m_OverlaySceneChache;
	TSelectBuffer m_OverlaySelectBuffer;
	TMouseEvent m_MouseEvent;
	
	static bool s_IsPicking;
};

}
#endif /*DISPLAY_ENGINE_H_*/
